package com.theexceptions.todo.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/**
 * An overlay of location pins to show where tasks are located.
 * 
 * @author Justian Meyer
 */
public class LocationItemizedOverlay extends ItemizedOverlay {

	protected ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();
	protected Activity activity;

	/**
	 * Constructs an overlay with a default marker image.
	 * 
	 * @param defaultMarker
	 * @param context
	 */
	public LocationItemizedOverlay(Drawable defaultMarker, Activity activity) {
		super(boundCenterBottom(defaultMarker));
		this.activity = activity;
	}

	/**
	 * Adds a new item to the overlay.
	 * 
	 * @param overlay
	 */
	public void addOverlay(OverlayItem overlay) {
		overlays.add(overlay);
		populate();
	}

	/**
	 * Overridden method to retrieve item from list when populating.
	 * 
	 * @param index Item index
	 * @return OverlayItem at given index
	 */
	@Override
	protected OverlayItem createItem(int index) {
		return overlays.get(index);
	}

	/**
	 * Handles actions when overlay item is tapped.
	 * 
	 * @param index Item index
	 * @return
	 */
	@Override
	protected boolean onTap(int index) {
		OverlayItem item = overlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
	}

	/**
	 * Returns the size of the array containing the OverlayItems.
	 * 
	 * @return number of overlays
	 */
	@Override
	public int size() {
		return overlays.size();
	}
}
